package utility;

public class VecHelper {
	public static vec2 direction(vec2 source, vec2 dest) {
		vec2 direction = new vec2(source.getX() - dest.getX(), source.getY()
				- dest.getY());
		return direction.normalize();
	}

	public static vec2 add(vec2 a, vec2 b) {
		return new vec2(a.getX() + b.getX(), a.getY() + b.getY());
	}

	public static float angleFromDirection(vec2 direction) {
		return (float) Math.atan2(direction.getY(), direction.getX());
	}

	public static float dot(vec2 a, vec2 b) {
		return ((a.getX() * b.getX()) + (a.getY() * b.getY()));
	}

	public static vec2 subtract(vec2 from, vec2 toThis) {
		return (new vec2(toThis.getX() - from.getX(), toThis.getY()
				- from.getY()));
	}

	public static float lerp(float a, float b, float delta) {
		return a + (b - a) * delta;
	}

	public static vec2 lerpPoints(vec2 a, vec2 b, float delta) {
		vec2 temp = new vec2();
		temp.setX(lerp(a.getX(), b.getX(), delta));
		temp.setY(lerp(a.getY(), b.getY(), delta));
		return temp;
	}
	
	public static float angleBetweenVectors(vec2 a, vec2 b)
	{
		float angle = 0.0f;
		
		float dotResult = dot(a, b);
		float lenResult = a.length() * b.length();
		angle = (float)Math.cos(dotResult/lenResult);
		
		return angle;
	}
	
	/**
	 * Converts degree to radian
	 * @param deg
	 * @return The degree value in radians
	 */
	public static float DegToRad(float deg)
	{
		return (float)(deg * (Math.PI/180));
	}
	
	/**
	 * Converts radians to degrees
	 * @param rad
	 * @return The radian value in degrees
	 */
	public static float RadToDeg(float rad)
	{
		return (float) (rad * (180/Math.PI));
	}
	
	/***
	 * Returns the rotated vector
	 * @param v
	 * @return v after rotation applied
	 */
	public static vec2 RotateVec2(vec2 v, double deg)
	{
		float x = v.getX();
		float y = v.getY();
		
		float cs = (float) Math.cos(deg);
		float sn = (float) Math.sin(deg);
		
		v.setX(x * cs - y * sn);
		v.setY(x * sn + y * cs);
		
		return v;
	}
}